package com.zoo.diannaopeijian.controller;

import com.zoo.diannaopeijian.common.page.Page;
import com.zoo.diannaopeijian.domain.Stock;
import com.zoo.diannaopeijian.domain.Ware;
import com.zoo.diannaopeijian.service.StockService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 库存控制器
 */
@Controller
@RequestMapping("/stock")
public class StockController extends BaseController {
    @Resource
    private StockService stockService;

    @GetMapping(value = "/pages/{pageName}")
    public String pageRoute(@PathVariable String pageName) {
        return "stock/" + pageName;
    }

//    @RequestMapping("/stockManaPage")
//    public String stockManaPageRoute() {
//        return "stock/stockManaPage";
//    }

    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> stockListGet(
            Page page,
            @RequestParam(required = true, defaultValue = "false") boolean isFuzzy,
            Integer componentId,
            String componentParameterDetailIds,
            Integer repertoryId,
            Ware ware
    ) {
        List<Integer> comParamDetails = new ArrayList<>();
        if (!StringUtils.isEmpty(componentParameterDetailIds)) {
            for (String detail : componentParameterDetailIds.split(" ")) {
                comParamDetails.add(Integer.parseInt(detail));
            }
        }
        return ResponseEntity.ok(
                buildPageResult(
                        isFuzzy ? stockService.getFuzzyStockWithPage(page, componentId, comParamDetails, repertoryId, ware)
                                : stockService.getWareStockWithPage(page, componentId, comParamDetails, repertoryId, ware)
                )
        );
    }

    @GetMapping("/stockNum")
    public ResponseEntity<Integer> stockNumGet(Stock stock) {
        return ResponseEntity.ok(stockService.getStockNum(stock));
    }

    @PutMapping("/")
    public ResponseEntity<String> stockPut(Stock stock) {
        return ResponseEntity.ok(stockService.updateStock(stock) ? success : fail);
    }
}
